package cn.wolfcode.luowowo.website_api.controller;

import cn.wolfcode.luowowo.QueryObject.TravelQuery;
import cn.wolfcode.luowowo.annotation.RequireLogin;
import cn.wolfcode.luowowo.annotation.UserParam;
import cn.wolfcode.luowowo.domain.*;
import cn.wolfcode.luowowo.redis.redisService.IStrategyStatisVoService;
import cn.wolfcode.luowowo.redis.redisService.ITravelStatisVoService;
import cn.wolfcode.luowowo.redis.redisService.IUserInfoRedisService;
import cn.wolfcode.luowowo.service.IDestinationService;
import cn.wolfcode.luowowo.service.IStrategyService;
import cn.wolfcode.luowowo.service.ITravelCommentService;
import cn.wolfcode.luowowo.service.ITravelService;
import cn.wolfcode.luowowo.util.*;
import cn.wolfcode.luowowo.vo.TravelStatisVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/travel")
public class TravelController {

    @Autowired
    private IDestinationService destinationService;
    @Autowired
    private ITravelService travelService;
    @Autowired
    private ITravelCommentService travelCommentService;
    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private IStrategyStatisVoService strategyStatisVoService;
    @Autowired
    private ITravelStatisVoService travelStatisVoService;


    @GetMapping("/query")
    public Object query(TravelQuery qo, @UserParam UserInfo userInfo) {
        Page<Travel> page = travelService.query(qo);
        return JsonResult.success(new ParamMap()
                .put("page", page)
                .put("userInfo", userInfo)
        );
    }

    //跳转游记编辑
    @GetMapping("/input")
    public Object input(String id) {
        ParamMap paramMap = new ParamMap();
        //更新时 过去当前的游记对象
        if (StringUtils.hasLength(id)) {
            Travel travel = travelService.get(id);
            paramMap.put("tv", travel);
        }
        //获取全部目的地
        List<Destination> destinations = destinationService.list();
        paramMap.put("dests", destinations);
        return JsonResult.success(paramMap);
    }

    //游记新增
    @PostMapping("/saveOrUpdate")
    @RequireLogin
    public Object saveOrUpdate(Travel travel, @UserParam UserInfo userInfo) {
        travel.setUserId(userInfo.getId());
        String id = travelService.saveOrUpdate(travel);
        return JsonResult.success(id);
    }

    //游记细节
    @GetMapping("/detail")
    public Object detail(String id, @UserParam UserInfo userInfo) {
        //detail
        Travel travel = travelService.get(id);
        //toast
        List<Destination> toasts = GetToast.getToastByDestId(travel.getDestId(), destinationService);
        //获取指定目的地下前三攻略  strategies
        List<Strategy> strategies = strategyService.getHighViewNumStrategy(travel.getDestId());
        //获取指定目的地下前三游记  travels
        List<Travel> travels = travelService.getHighViewNumTravel(travel.getDestId());

        //游记评论分页查询 并返回page给前端
        Page<TravelComment> page = CommentQueryUtil.
                TravelCommentQueryByStrategyId(id, travelCommentService);

        //每访问一次游记细节 使点击数 +1
        travelStatisVoService.viewnumIncrease(id, 1);

        //查询缓存中的统计数据对象vo 赋值给detail
        TravelStatisVO travelVo = travelStatisVoService.getTravelStatisVo(id);
        BeanUtils.copyProperties(travelVo, travel);

        return JsonResult.success(new ParamMap()
                .put("detail", travel)
                .put("toasts", toasts)
                .put("strategies", strategies)
                .put("travels", travels)
                .put("userInfo", userInfo)
                .put("page", page)
        );
    }

    @PostMapping("/commentAdd")
    @RequireLogin
    public Object commentAdd(TravelComment travelComment, @UserParam UserInfo userInfo) {
        //1.维护评论中的用户信息
        BeanUtils.copyProperties(userInfo, travelComment);
        travelComment.setUserId(userInfo.getId());
        //2.添加评论
        travelCommentService.addComment(travelComment);

        //3.游记评论分页查询 并返回page给前端
        Page<TravelComment> page = CommentQueryUtil.
                TravelCommentQueryByStrategyId(travelComment.getTravelId(), travelCommentService);

        //使统计数据评论数 +1
        travelStatisVoService.replynumIncrease(travelComment.getTravelId(),1);

        return JsonResult.success(page);
    }


    //上传图片
    @PostMapping("/contentImage")
    public String uploadUEImage(MultipartFile upfile, HttpServletRequest request) throws Exception {
        UMEditorUploader up = new UMEditorUploader(request);
        String[] fileType = {".gif", ".png", ".jpg", ".jpeg", ".bmp"};
        up.setAllowFiles(fileType);
        up.setMaxSize(10000); //单位KB
        up.upload(upfile);

        String callback = request.getParameter("callback");
        String result = "{\"name\":\"" + up.getFileName() + "\", \"originalName\": \"" + up.getOriginalName() + "\", \"size\": " + up.getSize()
                + ", \"state\": \"" + up.getState() + "\", \"type\": \"" + up.getType() + "\", \"url\": \"" + up.getUrl() + "\"}";
        result = result.replaceAll("\\\\", "\\\\");
        if (callback == null) {
            return result;
        } else {
            return "<script>" + callback + "(" + result + ")</script>";
        }
    }
}
